var app = getApp();
var api = require('../../api.js');
var WxParse = require('../../wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activity: {},
    id: 0,
    test_time: 30,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

    if (options) {
      var tmp_options = wx.getStorageSync("tmp_options");
      if (tmp_options) {
        wx.setStorageSync("tmp_options", Object.assign(tmp_options, options))
      } else {
        wx.setStorageSync("tmp_options", options)
      }
      if (options.id) {
        this.setData({
          id: options.id
        })
      }
    }
    this.getActivityDetail()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    var page = this
    var user = wx.getStorageSync('user_info');
    return {
      title: page.data.activity.title,
      path: '/pages/activity-details/activity-details?id=' + this.data.id + '&pid=' + user.user_id,
    }
  },
  getActivityDetail: function() {
    var page = this;
    wx.showLoading({
      title: '加载中',
    })
    app.request({
      url: api.activity.detail,
      method: 'POST',
      data: {
        id: page.data.id
      },
      success: function(res) {
        if (res.errcode == 0) {
          page.setData({
            activity: res.data,
            test_time: res.data.endjointime
          });
          page.setCountDown()
          wx.setStorageSync("join_activity", res.data.join);
          var content = res.data.detail + '<span> </span>'
          WxParse.wxParse("content", "html", content, page, 5);
        }
      },
      fail: function(res) {
        wx.showModal({
          title: '警告',
          content: res.msg,
          showCancel: false
        })
      },
      complete: function() {
        wx.hideLoading();
      }
    });
  },
  /**
   * 倒计时
   */
  setCountDown: function() {
    let page = this
    let time = 1;
    let test_time = page.data.test_time
    test_time -= time;
    if (test_time > 0) {
      let formatTime = page.getFormat(test_time);
      let countDown = `${formatTime.dd}天${formatTime.hh}时${formatTime.mm}分${formatTime.ss}秒`;
      page.setData({
        test_time: test_time,
        formatTime: formatTime,
        countDown: countDown
      });
      setTimeout(page.setCountDown, 1000);
    }

  },

  /**
   * 格式化时间
   */
  getFormat: function(msec) {
    let ss = parseInt(msec);
    // let ms = parseInt(msec % 1000);
    let mm = 0;
    let hh = 0;
    let dd = 0;
    if (ss > 60) {
      mm = parseInt(ss / 60);
      ss = parseInt(ss % 60);
      if (mm > 60) {
        hh = parseInt(mm / 60);
        mm = parseInt(mm % 60);
        if (hh > 24) {
          dd = parseInt(hh / 24);
          hh = parseInt(hh % 24);
        }
      }
    }
    ss = ss > 9 ? ss : `0${ss}`;
    mm = mm > 9 ? mm : `0${mm}`;
    hh = hh > 9 ? hh : `0${hh}`;
    dd = dd > 9 ? dd : `0${dd}`;
    return {
      ss,
      mm,
      hh,
      dd
    };
  },

})